package top.xana.acg.music

import android.animation.ObjectAnimator
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.TextView
import top.xana.acg.common.base.ui.BaseActivity
import top.xana.acg.music.databinding.ActivityMusicBinding

class MusicActivity : BaseActivity() {

    override fun getLayoutId() = R.layout.activity_music

    private val b by lazy {
        getBind<ActivityMusicBinding>()
    }

    override fun initView() {
        super.initView()
        createSpannableString()
        b.c = C {}
    }

    private fun createSpannableString() {
        SpannableString(getString(R.string.lab_private_strategy)).let {
            it.setSpan(
                ForegroundColorSpan(Color.parseColor("#DDDDDD")),
                0,
                2,
                Spanned.SPAN_EXCLUSIVE_INCLUSIVE
            )
            it.setSpan(C { toast("go protocal") }, 2, 8, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
            it.setSpan(C { toast("go private") }, 8, 14, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
            it.setSpan(C { toast("go children") }, 14, 22, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
            b.tvPrivateStrategy.apply {
                text = it
                movementMethod = LinkMovementMethod.getInstance()
                highlightColor = Color.TRANSPARENT
                setOnClickListener {
                    if (selectionStart == -1 && selectionEnd == -1)
                        it.isSelected = !it.isSelected
                }
            }
        }
    }

    inner class C(val func: () -> Unit) : ClickableSpan() {
        override fun onClick(p0: View) {
            func()
        }

        override fun updateDrawState(ds: TextPaint) {
            ds.color = Color.WHITE
        }

        fun multiLogin(i: Int) {
            b.tvPrivateStrategy.also {
                if (it.isSelected) {
                    when (i) {
                        -2 -> toast("go login")
                        -1 -> {
                            toast("go enjoy")
                            navTo(MainActivity::class.java)
                        }
                        0 -> toast("微信")
                        1 -> toast("QQ")
                        2 -> toast("微博")
                        3 -> toast("网易云")
                    }
                } else {
                    toast("请先勾选${it.text}")
                    ObjectAnimator.ofFloat(
                        it,
                        "translationX",
                        0f,
                        25f,
                        -25f,
                        25f,
                        -25f,
                        15f,
                        -15f,
                        6f,
                        -6f,
                        0f
                    ).also {
                        it.duration = 1000
                    }.start()
                }
            }
        }
    }

}
